package com.doform.android.xianggucheng.service;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.doform.android.xianggucheng.help.AppHelp;
import com.doform.android.xianggucheng.utils.Text2SpeechUtils;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.orhanobut.logger.Logger;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017/7/5.
 * GitHub：https://github.com/cxydxpx
 * <p>
 * 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>
 * onReceiveMessageData 处理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理回执 <br>
 */

public class TSIntentService extends GTIntentService {

    private Text2SpeechUtils text2SpeechUtils;

    @Override
    public void onReceiveServicePid(Context context, int i) {
        Logger.v(i + "");
    }

    /**
     * 接收ClientId
     *
     * @param context
     * @param clientid
     */
    @Override
    public void onReceiveClientId(Context context, String clientid) {
//        Logger.v("接收 clientid -- " + clientid);

        String mName = AppHelp.getInstance().getUserName();
        if (!TextUtils.isEmpty(mName)) {
            boolean b = PushManager.getInstance().bindAlias(this, AppHelp.getInstance().getUserName());
            if (b) {
                Logger.v("绑定成功");
            } else {
                PushManager.getInstance().bindAlias(this, AppHelp.getInstance().getUserName());
                Logger.v("绑定失败");
            }
        }
    }

    /**
     * 处理透传消息
     *
     * @param context
     * @param msg
     */
    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        Logger.v("接收处理透传消息");

        if (text2SpeechUtils == null) {
            text2SpeechUtils = new Text2SpeechUtils(context);
        }
        text2SpeechUtils.text2Speech("您有新订单，请注意查收");

        String appid = msg.getAppid();
        String taskid = msg.getTaskId();
        String messageid = msg.getMessageId();
        byte[] payload = msg.getPayload();
        String pkg = msg.getPkgName();
        String cid = msg.getClientId();
        boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
        Log.d(TAG, "call sendFeedbackMessage = " + (result ? "success" : "failed"));
        if (payload == null) {
        } else {
            String data = new String(payload);
//            sendMessage(data, 0);
            System.out.println("data-------->" + data);
        }

    }

    /**
     * 离线上线通知
     *
     * @param context
     * @param b
     */
    @Override
    public void onReceiveOnlineState(Context context, boolean b) {
//        Logger.v("离线上线通知" + b + "");
    }

    /**
     * 各种事件处理回执
     *
     * @param context
     * @param gtCmdMessage
     */
    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {
        Logger.v("各种消息处理回执" + gtCmdMessage.getPkgName());
    }
}
